package list

import (
	C "gitee.com/ljfirst/algo-go-sdk/common/constant"
)

/**
 * @author ljfirst
 * @version V1.0
 * @date 2023/6/28 17:03
 * @author-Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description
 * */
type DuplexList struct {
	head       *Node // 带有头节点的list
	tail       *Node
	size       int
	headInsert bool // 头插法，默认false是尾插法
}

func NewDuplexList(options ...C.Options) *DuplexList {
	opt := C.NewOptions(options...)
	temp := &Node{}
	list := &DuplexList{
		head: temp,
		tail: temp,
		size: 0,
	}
	list.headInsert = opt.HeadInsert
	return list
}

func (m *DuplexList) Insert(node ...*Node) { // 尾插入
	if node == nil || len(node) == 0 {
		return
	}
	if m.headInsert {
		for i := 0; i < len(node); i++ {
			node[i].Next = m.head.Next
			m.head.Next = node[i]
		}
		// 链表为空时的首个元素处理
		if m.head.Next == nil {
			m.tail = node[0]
		}
		return
	}
	// 尾插法
	for i := 0; i < len(node); i++ {
		m.tail.Next = node[i]
		m.tail = node[i]
	}
}

func (m *DuplexList) InsertIndex(index int, nodes ...*Node) { // index 起始为0
	for _, n := range nodes {
		node := n
		if node == nil {
			return
		}
		// 空链表插入首个节点
		if index == 0 && m.head == nil {
			m.Insert(node)
			return
		}
		// 插入首个节点
		if index == 0 {

		}
		if index >= m.size {
			return
		}
		count := 0
		point := m.head
		for count < index {
			point = point.Next
			count++
		}
		if point.Next != nil {
			next := point.Next
			next.Pre = node
			node.Next = next
		}
		point.Next = node
		node.Pre = point
	}
}

func (m *DuplexList) Delete(node *Node) {}
func (m *DuplexList) DeleteIndex(int)   {}

func (m *DuplexList) ValueSet() []interface{} {
	return nil
}
func (m *DuplexList) Equals(node *Node) {}
func (m *DuplexList) Size() int         { return 0 }
func (m *DuplexList) Clear()            {}

func (m *DuplexList) GetAttribute() *C.Attribute {
	return &C.Attribute{
		Tags: []string{C.List},
		Desc: &C.Desc{
			Name:   "DuplexList",
			NameCn: "双向链表",
		},
	}
}